﻿Public Class Form2
    Private Dr As New Draw
    Private SF As New StringFormat
    Public SL As New List(Of String)
    Private Rd As New Random(Now.Second)
    Public _S As String = ""
    Public _T As New JSetting
    Private WeekD As Integer = Now.Date.DayOfWeek
    Private _OldIcoName As String = "wkico.ico"
    Public Structure JSetting
        Public _sFont As Font
        Public _LgColor1 As Color
        Public _LgColor2 As Color
        Public IsShadow As Boolean
        Public IsOutLine As Boolean
        Public TimeInt As Integer
    End Structure
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
        Me.NotifyIcon1.BalloonTipTitle = "DS名言警句"
        Me.NotifyIcon1.BalloonTipText = "欢迎使用DS名言警句"
        Me.NotifyIcon1.ShowBalloonTip(10000)
        SetShortCut()
        Dim S As String = My.Computer.FileSystem.SpecialDirectories.Temp
        Dim S2 As String = My.Computer.FileSystem.GetParentPath(Application.ExecutablePath)
        If S2.Contains(S) = True Then
            MsgBox("友情提示:" & vbCrLf & "为更下一次方便使用并且打开更快速,建议不要直接在压缩包中双击打开本程序!" & vbCrLf & "建议您先将本程序解压或从压缩包中拖放至其他地方再打开!", vbOKOnly + vbInformation)
        End If
        Dim SS1 As String = GetSetting("DS名言警句", "Setting", "CustomLines", "")
        If SS1.Length <> 0 Then
            If SS1 = "True" Then
                Dim pth As String = Application.StartupPath
                pth = IIf(pth.EndsWith("\"), pth, pth & "\")
                If IO.File.Exists(pth & "名言警句语句库.txt") = True Then
                    Dim S1 As String = IO.File.ReadAllText(pth & "名言警句语句库.txt", System.Text.Encoding.Default)
                    If S1.Trim.Length <> 0 Then
                        SL.AddRange(S1.Split(vbCrLf))
                    Else
                        SL.AddRange(My.Resources.Strings.Split(vbCrLf))
                    End If
                Else
                    SL.AddRange(My.Resources.Strings.Split(vbCrLf))
                End If
            Else
                SL.AddRange(My.Resources.Strings.Split(vbCrLf))
            End If
        Else
            SL.AddRange(My.Resources.Strings.Split(vbCrLf))
        End If
        SetFormCanMove(Me)
        SF.LineAlignment = StringAlignment.Near
        SF.Alignment = StringAlignment.Far
        Try
            Dim SP As String = GetSetting("DS名言警句", "FormPoint", "Location", "")
            If SP.Length <> 0 AndAlso SP.Split("|").Length = 2 Then
                Dim K() As String = SP.Split("|")
                Me.Location = New Point(K(0), K(1))
            End If
        Catch
        End Try
        With _T
            Dim SS As String = GetSetting("DS名言警句", "Setting", "_LgColor1", "")
            If SS.Length <> 0 AndAlso SS.Split(",").Length = 3 Then
                Dim SP() As String = SS.Split(",")
                ._LgColor1 = Color.FromArgb(255, SP(0), SP(1), SP(2))
            Else
                ._LgColor1 = Color.White
            End If
            SS = GetSetting("DS名言警句", "Setting", "_LgColor2", "")
            If SS.Length <> 0 AndAlso SS.Split(",").Length = 3 Then
                Dim SP() As String = SS.Split(",")
                ._LgColor2 = Color.FromArgb(255, SP(0), SP(1), SP(2))
            Else
                ._LgColor2 = Color.DarkGray
            End If
            Dim Fz As Integer = 14
            SS = GetSetting("DS名言警句", "Setting", "ftSize", "")
            If SS.Length <> 0 Then
                Fz = SS
            End If
            SS = GetSetting("DS名言警句", "Setting", "_sFont", "")
            If SS.Length <> 0 Then
                ._sFont = New Font(SS, Fz, FontStyle.Regular, GraphicsUnit.Point)
            Else
                ._sFont = New Font("楷体", Fz, FontStyle.Regular, GraphicsUnit.Point)
            End If
            SS = GetSetting("DS名言警句", "Setting", "IsOutLine", "")
            If SS.Length <> 0 Then
                If SS = "True" Then
                    .IsOutLine = True
                Else
                    .IsOutLine = False
                End If
            Else
                .IsOutLine = True
            End If
            SS = GetSetting("DS名言警句", "Setting", "IsShadow", "")
            If SS.Length <> 0 Then
                If SS = "True" Then
                    .IsShadow = True
                Else
                    .IsShadow = False
                End If
            Else
                .IsShadow = True
            End If
            SS = GetSetting("DS名言警句", "Setting", "TimeInt", "")
            If SS.Length <> 0 Then
                .TimeInt = SS
            Else
                .TimeInt = 10
            End If
        End With
    End Sub
    Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H80000
            Me.WindowState = FormWindowState.Normal
            Return cp
        End Get
    End Property
    ''' <summary>
    ''' 显示名言警句
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub SetText()
        Timer1.Enabled = False
        Timer1.Interval = _T.TimeInt * 1000
        Try
            Dim R As Integer = Rd.Next(0, SL.Count)
            R = IIf(R >= SL.Count, R - 1, R)
            R = IIf(R <= 1, 1, R)
            Try
                _S = SL(R).Replace(vbCr, "").Replace(vbLf, "").Trim
            Catch
                Timer1.Enabled = True
            End Try
            If _S.Length <> 0 Then
                If Dr.ImageSource2 IsNot Nothing Then Dr.ImageSource2.Dispose()
                Dim WW As Integer = Screen.GetWorkingArea(New Point(0, 0)).Width / 2
                If Dr.ImageSource1 Is Nothing Then
                    Dr.ImageSource1 = New Bitmap(WW, 200)
                End If
                Using _B As New Bitmap(WW, 200)
                    Using G As Graphics = Graphics.FromImage(_B)
                        G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                        Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, _T._sFont.Height), _T._LgColor1, _T._LgColor2)
                            If _T.IsOutLine = True Then
                                Dr.DrawTextOutLine(Color.Black, G, _S, SF, _T._sFont, New Size(WW, 200))
                            End If
                            G.DrawString(_S, _T._sFont, Lg, New Rectangle(0, 0, WW, 200), SF)
                        End Using
                    End Using
                    If _T.IsShadow = True Then
                        Dr.ImageSource2 = New Bitmap(Dr.DrawShadow(_B))
                    Else
                        Dr.ImageSource2 = New Bitmap(_B)
                    End If
                    For I As Integer = 0 To WW Step 5
                        DrawBP(Me, Dr.MakeTransparentImage(I), 255)
                        Application.DoEvents()
                    Next
                    Console.WriteLine(_S)
                    If Dr.ImageSource1 IsNot Nothing Then Dr.ImageSource1.Dispose()
                    Dr.ImageSource1 = New Bitmap(Dr.ImageSource2)
                End Using
            End If
        Catch
        End Try
        Timer1.Enabled = True
    End Sub

    Private Sub 退出XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出XToolStripMenuItem.Click
        Me.NotifyIcon1.Dispose()
        End
    End Sub
    Private Sub 设置SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 设置SToolStripMenuItem.Click
        设置.TabControl1.SelectedIndex = 0
        设置.ShowDialog()
    End Sub
    Private Sub 关于AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于AToolStripMenuItem.Click
        设置.TabControl1.SelectedIndex = 2
        设置.ShowDialog()
    End Sub

    Private Sub Form2_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        Try
            SaveSetting("DS名言警句", "FormPoint", "Location", Me.Left & "|" & Me.Top)
        Catch
        End Try
    End Sub
    Private Sub Form2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        If e.Button = MouseButtons.Right Then
            Me.ContextMenuStrip1.Show(Me, e.Location)
        End If
    End Sub

    Private Sub Form2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
    End Sub

    Private Sub Form2_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Dim R As Integer = Rd.Next(0, SL.Count)
        R = IIf(R >= SL.Count, R - 1, R)
        R = IIf(R <= 1, 1, R)
        Try
            _S = SL(R).Replace(vbCr, "").Replace(vbLf, "").Trim
        Catch
            Timer1.Enabled = True
        End Try
        If _S.Length <> 0 Then
            If Dr.ImageSource2 IsNot Nothing Then Dr.ImageSource2.Dispose()
            Dim WW As Integer = Screen.GetWorkingArea(New Point(0, 0)).Width / 2
            If Dr.ImageSource1 Is Nothing Then
                Dr.ImageSource1 = New Bitmap(WW, 200)
            End If
            Using _B As New Bitmap(WW, 200)
                Using G As Graphics = Graphics.FromImage(_B)
                    G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                    Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, _T._sFont.Height), _T._LgColor1, _T._LgColor2)
                        Dr.DrawTextOutLine(Color.Black, G, _S, SF, _T._sFont, New Size(WW, 200))
                        G.DrawString(_S, _T._sFont, Lg, New Rectangle(0, 0, WW, 200), SF)
                    End Using
                End Using
                Dr.ImageSource2 = New Bitmap(Dr.DrawShadow(_B))
                Me.Left = Screen.GetWorkingArea(New Point(0, 0)).Width - WW - 100
                For I As Integer = 0 To WW Step 5
                    DrawBP(Me, Dr.MakeTransparentImage(I), 255)
                    Application.DoEvents()
                Next
                If Dr.ImageSource1 IsNot Nothing Then Dr.ImageSource1.Dispose()
                Dr.ImageSource1 = New Bitmap(Dr.ImageSource2)
            End Using
            Timer1.Enabled = True
        End If
    End Sub

    Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim S As String = _S.Replace(vbCr, "").Replace(vbLf, "").Trim
        Try
            Me.NotifyIcon1.Text = IIf(S.Length >= 21, S.Substring(0, 20) & "...", S)
        Catch
        End Try
        If WeekD <> Now.Date.DayOfWeek Then
            Console.WriteLine(WeekD)
            SetShortCut()
            WeekD = Now.Date.DayOfWeek
        End If
        SetText()
        Try
            ReduceMemory()
        Catch
        End Try
    End Sub
    Private Sub ReduceMemory()
        Dim A As Process = Process.GetCurrentProcess()
        A.MaxWorkingSet = Process.GetCurrentProcess.MaxWorkingSet
        A.Dispose()
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick

    End Sub

    Private Sub NotifyIcon1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDown
        If Me.WindowState <> FormWindowState.Normal Then Me.WindowState = FormWindowState.Normal
    End Sub

    Private Sub 置顶TToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 置顶TToolStripMenuItem.Click
        置顶TToolStripMenuItem.Checked = Not 置顶TToolStripMenuItem.Checked
        Me.TopMost = 置顶TToolStripMenuItem.Checked
    End Sub
    Private Sub SetShortCut()
        Try
            Dim tmp As String = My.Computer.FileSystem.SpecialDirectories.Temp
            tmp = IIf(tmp.EndsWith("\"), tmp, tmp & "\")
            If IO.File.Exists(tmp & _OldIcoName) Then
                Try
                    IO.File.Delete(tmp & _OldIcoName)
                Catch
                End Try
            End If
            _OldIcoName = "wkico" & Now.Second & ".ico"
            Dr.CreateShortcut("DS名言警句", Application.ExecutablePath, "", "DYLIKE提示您:" & vbCrLf & "今天是:" & Dr.GetWeekString, tmp)
            Dim stm As New IO.FileStream(tmp & _OldIcoName, IO.FileMode.Create)
            Dr.GetWeekICON.Save(stm)
            stm.Close()
            Dr.CreateShortcut("DS名言警句", Application.ExecutablePath, tmp & _OldIcoName, "DYLIKE提示您:" & vbCrLf & "今天是:" & Dr.GetWeekString, tmp)
        Catch
        End Try
    End Sub
End Class